<template>
  <div><br>
    <div>
    <el-button type="success" @click="PolymerizationGoodsAdd">新增聚合商品</el-button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <el-input
      fixed="right"
      v-model="list.polymerizationGoodsName"
      placeholder="请输入聚合名称"
      style="width: 200px"
    />
    <el-button  @click="getlist()" :icon="Search" circle />
  </div><br>
    <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="polymerizationGoodsName" label="聚合名称" width="600" />
    <el-table-column  label="操作列" min-width="120">
      <template #default="{row}">
        <el-button link type="primary" size="small" @click="Updates(row)">
          编辑
        </el-button>
        <el-button link type="primary" size="small" @click="Deleteds(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  
  <el-dialog v-model="dialogFormVisible" title="新增聚合商品" width="500">
    <el-form :model="form">
      <el-form-item label="商品名称" :label-width="80">
        <el-input v-model="form.polymerizationGoodsName" autocomplete="off" />
      </el-form-item>
      
      <el-upload
    class="avatar-uploader"
    action="https://localhost:44392/PostImags"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="form.polymerizationGoodsImg" :src="form.polymerizationGoodsImg" class="avatar" style="width:100px;height: 100px;"/>
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>


    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="clickadd()">
          确认新增
        </el-button>
      </div>
    </template>
  </el-dialog>


  <el-dialog v-model="dialogFormVisibles" title="修改聚合商品" width="500">
    <el-form :model="forms">
      <el-form-item label="商品名称" :label-width="80">
        <el-input v-model="forms.polymerizationGoodsName" autocomplete="off" />
      </el-form-item>
      
      <el-upload
    class="avatar-uploader"
    action="https://localhost:44392/PostImags"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="forms.polymerizationGoodsImg" :src="forms.polymerizationGoodsImg" class="avatar" style="width:100px;height: 100px;"/>
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>


    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisibles = false">取消</el-button>
        <el-button type="primary" @click="clickupdate()">
          确认修改
        </el-button>
      </div>
    </template>
  </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref,onMounted} from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import type { UploadProps } from 'element-plus'
import {Search} from '@element-plus/icons-vue'

const router = useRouter()
const tableData = ref([])

const dialogFormVisible = ref(false)
const dialogFormVisibles = ref(false)
const form=ref({
  polymerizationGoodsName:"",
  polymerizationGoodsImg:""
})

const forms=ref({
  id:"",
  polymerizationGoodsName:"",
  polymerizationGoodsImg:""
})


const list=ref({
  polymerizationGoodsName:""
})


//图片上传
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response
) => {
  form.value.polymerizationGoodsImg=response
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('图片必须是JPG类型!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片不能大于2mb!')
    return false
  }
  return true
}

//生命周期函数
onMounted(()=>{
  getlist()
})


//获取聚合商品列表
const getlist=()=>{
  axios({
    url:"https://localhost:44392/GetPolymerizationGoods",
    method:"get",
    params:list.value
  }).then(res=>{
    tableData.value=res.data.data
  })
}

//新增按钮
const PolymerizationGoodsAdd=()=>{
  dialogFormVisible.value=true
}

//确认新增按钮
const clickadd=()=>{
  axios({
    url:"https://localhost:44392/PostPolymerizationGoods",
    method:"post",
    data:form.value
  }).then(res=>{
    if(res.data.code==200)
  {
    ElMessage({
      message: '新增成功',
      type: 'success',
    })
    dialogFormVisible.value=false
    getlist()
  }
  else
  {
    ElMessage.error('新增失败')
  }
  })
}

//删除按钮
const Deleteds=(id:any)=>{
 axios({
  url:"https://localhost:44392/DeletePolymerizationGoods",
  method:"delete",
  params:{
    id:id
  }
 }).then(res=>{
  if(res.data.code==200)
 {
  ElMessage({
    message: '删除成功',
    type: 'success',
  })
  
  getlist()
 }
 })
}

//修改按钮
const Updates=(row:any)=>{
  dialogFormVisibles.value=true
  forms.value.id=row.id
  forms.value.polymerizationGoodsName=row.polymerizationGoodsName
  forms.value.polymerizationGoodsImg=row.polymerizationGoodsImg
  console.log(forms.value.polymerizationGoodsImg)
}

//确认修改按钮
const clickupdate=()=>{
  axios({
    url:"https://localhost:44392/PutPolymerizationGoods",
    method:"put",
    data:forms.value
  }).then(res=>{
    if(res.data.code==200) 
  {
    ElMessage({
      message: '修改成功',
      type: 'success',
    })

    dialogFormVisibles.value=false
    getlist()
  }
})
}








</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
